﻿using DataModel;
using Helpers;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Navigation;

namespace ArmyCreator
{
    public class UserContext : INotifyPropertyChanged
    {
        #region SINGLETON
        private static UserContext _instance = new UserContext();
        public static UserContext Instance
        {
            get
            {
                return _instance;
            }
        }
        #endregion

        private UserContext()
        {
            IsOnline = false;
            if (IsolatedStorageHelper.ContainsKey("listings"))
            {
                Listings = IsolatedStorageHelper.GetObject<ObservableCollection<Listing>>("listings");
                Notify("LastListing");
            }
            else
            {
                Listings = new ObservableCollection<Listing>();
                Notify("LastListing");
            }

            listings.CollectionChanged += CollectionChangedHandler;
        }

        private void CollectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e)
        {
            IsolatedStorageHelper.SaveObject("listings", listings);
            Notify("LastListing");
        }

        public bool IsOnline { get; set; }

        public string Login
        {
            get { return IsolatedStorageHelper.GetObject<string>("login"); }
            set { IsolatedStorageHelper.SaveObject("login", value); Notify("Login"); }
        }

        public string Password
        {
            get { return IsolatedStorageHelper.GetObject<string>("password"); }
            set { IsolatedStorageHelper.SaveObject("password", value); Notify("Password"); }
        }

        public bool LoadAllAtStartup
        {
            get { return IsolatedStorageHelper.GetObject<bool>("loadAllAtStartup"); }
            set { IsolatedStorageHelper.SaveObject("loadAllAtStartup", value); Notify("LoadAllAtStartup"); }
        }

        private ObservableCollection<Listing> listings;

        public ObservableCollection<Listing> Listings
        {
            get { return listings; }
            set { listings = value; Notify("Listings"); }
        }

        public Listing LastListing
        {
            get { return listings.LastOrDefault(); }
        }

        public Listing GetListing(int id)
        {
            foreach (var listing in Listings)
            {
                if (listing.Army.Id == id)
                {
                    return listing;
                }
            }

            return null;
        }


        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void Notify(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
